﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryFramework
{
	/// <summary>
	/// Class <see cref="UnitOfWorkProvider"/> define unit of work provider
	/// </summary>
	public abstract class UnitOfWorkProvider<T> : IUnitOfWorkProvider<T> where T : IUnitOfWork
	{

		#region Fields
		private T _readOnly = default(T); 
		#endregion

		#region Methods

		/// <summary>
		/// Gets the read only.
		/// </summary>
		/// <returns></returns>
		public T GetReadOnly()
		{
			if (_readOnly == null)
			{
				_readOnly = GetNew();
			}
			return _readOnly;
		}

		/// <summary>
		/// Gets the transactional.
		/// </summary>
		/// <returns></returns>
		public T GetTransactional()
		{
			return GetNew();
		}

		/// <summary>
		/// Gets the new unit of work.
		/// </summary>
		/// <returns></returns>
		protected abstract T GetNew();

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		public void Dispose()
		{
			if (_readOnly != null)
			{
				_readOnly.Dispose();
			}
		}

		#endregion
	}
}
